

#import "HLTabBarController.h"

@interface HLTabBarController ()<HLTabBarDelegate>

@end

@implementation HLTabBarController

/*************************************  HLTabBarController *************************************/

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.tabBar addSubview:self.customTabBar];
}



- (void)viewWillLayoutSubviews
{
    [super viewWillLayoutSubviews];
    self.customTabBar.frame = self.tabBar.bounds;
    //删除tabBar中自带的子控件
    for (UIView *view in self.tabBar.subviews) {
        //UITabBarButton是UITabBarController中各个子控制器在工具条中对应的按钮的称呼
        if ([view isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            [view removeFromSuperview];
        }
    }
}




#pragma mark -tabar代理方法
- (void)tabBar:(HLTabBar *)tabBar didSelectedButtonFrom:(NSInteger)from to:(NSInteger)to
{
    self.selectedIndex = to;
}




/**初始化一个子控制器*/
- (void)setupChildViewController:(UIViewController *)childVC title:(NSString *)title imageName:(NSString *)imageName selectedImageName:(NSString *)selectedImageName
{
    
    //1 add
    [self addChildViewController:childVC];

    //2 设置子控制器属性
    childVC.title = title;
    childVC.tabBarItem.image = [UIImage imageNamed:imageName];
    UIImage *selectedImage = [UIImage imageNamed:selectedImageName];
    
    if([[UIDevice currentDevice].systemVersion doubleValue] >= 7.0){ //图片不需要进行渲染
        childVC.tabBarItem.selectedImage = [selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        
    }else{
        childVC.tabBarItem.selectedImage = selectedImage;
    }
    
    //3 添加TabBar内部的按钮( UITabBarButton⾥面显⽰什么内容,由对应子控制器的tabBarItem属性来决定)
    [self.customTabBar addTabBarButtonWithItem:childVC.tabBarItem];
    
}




//设置设备方向
//是否支持旋转
-(BOOL)shouldAutorotate{
    return self.selectedViewController.shouldAutorotate;
}

//支持的方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return self.selectedViewController.supportedInterfaceOrientations;
}


#pragma mark -- setter&&getter

- (HLTabBar *)customTabBar
{
    if(_customTabBar == nil){
        _customTabBar = [[HLTabBar alloc] init];
        _customTabBar.delegate = self;
        _customTabBar.backgroundColor = [UIColor whiteColor];
    }
    return _customTabBar;
}

@end



/*************************************  HLTabBar *************************************/
@interface HLTabBar()
/**当前选中的按钮*/
@property (nonatomic,weak) SPVerticalLayoutBtn *selectedButton;

@end

@implementation HLTabBar

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if(self){
        //只需要设置一次的属性
    }
    return self;
}



//在TabBar里添加按钮
- (void)addTabBarButtonWithItem:(UITabBarItem *)item
{
    SPVerticalLayoutBtn *button = [SPVerticalLayoutBtn new];
    button.textColor_nomal = _item_textColor_nomal;
    button.textColor_selected = _item_textColor_selected;
    button.font = _item_font;
    button.tag = item.tag;
    [self addSubview:button];
    [self.tabBarButtons addObject:button];
    button.item = item;
    [button addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchDown];
    //默认第一个按钮为选中
    if(self.tabBarButtons.count == 1){
        [self clickButton:button];
    }
    
}


//监听按钮点击
- (void)clickButton:(SPVerticalLayoutBtn *)button
{
    self.selectedButton.selected = NO;
    button.selected = YES;
    
    if([self.delegate respondsToSelector:@selector(tabBar:didSelectedButtonFrom:to:)]){
        [self.delegate tabBar:self didSelectedButtonFrom:self.selectedButton.tag to:button.tag];
    }
    
    self.selectedButton = button;
}




//布局子控件
- (void)layoutSubviews
{
    [super layoutSubviews];
    
    //设置frame
    CGFloat height = 49;
    CGFloat width = self.frame.size.width;
    CGFloat buttonY = 0;
    CGFloat buttonW= width / self.tabBarButtons.count;
    CGFloat buttonH = height;
    
    for (int index = 0; index < self.tabBarButtons.count; index++) {
        SPVerticalLayoutBtn *button = self.tabBarButtons[index];
        CGFloat buttonX = index * buttonW;
        button.frame = CGRectMake(buttonX, buttonY, buttonW, buttonH);
        button.tag = index;
    }
}


#pragma mark -- setter&&getter

- (NSMutableArray *)tabBarButtons
{
    if(_tabBarButtons == nil){
        _tabBarButtons = [NSMutableArray array];
    }
    return _tabBarButtons;
}

@end



/*************************************  SPVerticalLayoutBtn *************************************/
@interface SPVerticalLayoutBtn()
@end

@implementation SPVerticalLayoutBtn

//只需要设置一次的属性
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if(self){
        
        //设置文字居中
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
        
        
        //        self.imageView.backgroundColor = [UIColor yellowColor];
        //        self.titleLabel.backgroundColor = [UIColor redColor];
        
        //        //添加新消息提示
        //        _news_btn = [UIButton new];
        //        _news_btn.frame = CGRectMake(0, -4, 20, 20);
        //        _news_btn.hidden = YES;
        //        _news_btn.titleLabel.font = [UIFont systemFontOfSize:fontSize];
        //        _news_btn.backgroundColor = [UIColor redColor];
        //        _news_btn.layer.cornerRadius = _news_btn.frame.size.width * 0.5;
        //        _news_btn.clipsToBounds = YES;
        //        [self addSubview:_news_btn];
        
    }
    
    return self;
}


//设置item
-(void)setItem:(UITabBarItem *)item
{
    _item = item;
    //设置文字
    [self setTitle:item.title forState:UIControlStateNormal];
    [self setTitle:item.title forState:UIControlStateSelected];
    [self setTitleColor:_textColor_nomal forState:UIControlStateNormal];
    [self setTitleColor:_textColor_selected forState:UIControlStateSelected];
    self.titleLabel.font = _font;
    
    //设置图片
    [self setImage:item.image forState:UIControlStateNormal];
    [self setImage:item.selectedImage forState:UIControlStateSelected];
}



//重写去掉高亮状态
-(void)setHighlighted:(BOOL)highlighted{};


- (void)layoutSubviews
{
    [super layoutSubviews];
    CGRect rect_imageV = self.imageView.frame;
    CGRect rect_label = self.titleLabel.frame;
    
    rect_imageV.origin.x = (self.frame.size.width - rect_imageV.size.width)*0.5;
    rect_imageV.origin.y = 10;
    
    rect_label.origin.x = (self.frame.size.width - rect_label.size.width)*0.5;
    rect_label.origin.y = CGRectGetMaxY(rect_imageV)+4;
    
    self.imageView.frame = rect_imageV;
    self.titleLabel.frame = rect_label;
}

@end
